package auto.app.controller;


import auto.app.controller.from.DatabaseConnect.DatabaseConnectionAdd;
import auto.app.controller.from.DatabaseConnect.DatabaseConnectionUpdate;
import auto.app.controller.info.BeforeAssertion.BeforeAssertionInfo;
import auto.app.enums.DatabaseConnectType;
import auto.app.exetion.MainExection;
import auto.app.model.DatabaseConnect;
import auto.app.service.DatabaseConnectService;
import auto.app.util.CommonUtil;
import auto.app.util.MysqlUtil;
import auto.app.util.Result;
import auto.app.vo.MysqlReturn;
import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

import static auto.app.exetion.MainExetionMessage.CONNECTIONSLECTERROR;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 杨森
 * @since 2022-10-17
 */
@RestController
@RequestMapping("/databaseConnect")
@Api(value = "数据库操作",tags = "数据库操作")
@AllArgsConstructor
public class DatabaseConnectController {

    DatabaseConnectService databaseConnectService;
    MysqlUtil mysqlUtil;

    @GetMapping(value = "/type",name = "获取数据库操作类型")
    @ApiOperation(value = "获取数据库操作类型",notes = "获取数据库操作类型")
    public BeforeAssertionInfo beforeAssertionMethodType(){
        List<Map<String, String>> maps = DatabaseConnectType.toList();
        BeforeAssertionInfo beforeAssertionInfo=new BeforeAssertionInfo();
        beforeAssertionInfo.setCode(200);
        beforeAssertionInfo.setMsg("请求成功");
        beforeAssertionInfo.setData(maps);
        return beforeAssertionInfo;
    }

    @PostMapping(name = "新增数据库操作")
    @ApiOperation(value = "新增数据库操作",notes = "新增数据库操作")
    public Result<Boolean> addDatabaseConnect(@Valid @RequestBody DatabaseConnectionAdd databaseConnectionAdd) throws MainExection {
        return Result.getInstance(Boolean.class).setData(databaseConnectService.addDatabaseConnect(databaseConnectionAdd));
    }

    @DeleteMapping(value = "/{databaseId}",name = "删除数据库操作")
    @ApiOperation(value = "删除数据库操作",notes = "删除数据库操作")
    public Result<Boolean> deleteDatabaseConnect(@PathVariable String databaseId) throws MainExection {
        return Result.getInstance(Boolean.class).setData(databaseConnectService.deleteDatabaseConnect(databaseId));
    }
    @GetMapping(value = "/detail/{databaseId}",name = "数据库操作详情")
    @ApiOperation(value = "数据库操作详情",notes = "数据库操作详情")
    public Result<DatabaseConnect> detailDatabaseConnect(@PathVariable String databaseId) throws MainExection {
        DatabaseConnect connectServiceById = databaseConnectService.getById(databaseId);
        if (ObjectUtil.isNull(connectServiceById)){
            throw new MainExection(CONNECTIONSLECTERROR.getCode(),CONNECTIONSLECTERROR.getMsg());
        }
        return Result.getInstance(DatabaseConnect.class).setData(connectServiceById);
    }

    @PutMapping(name = "修改数据库操作")
    @ApiOperation(value = "修改数据库操作",notes = "修改数据库操作")
    public Result<Boolean> updateDatabaseConnect(@Valid @RequestBody DatabaseConnectionUpdate databaseConnectionUpdate) throws MainExection {
        return Result.getInstance(Boolean.class).setData(databaseConnectService.updateDatabaseConnect(databaseConnectionUpdate));
    }

    @GetMapping(value = "/list/{caseId}",name = "数据库操作列表")
    @ApiOperation(value = "数据库操作列表",notes = "数据库操作列表")
    public Result<List<DatabaseConnect>> listDatabaseConnect(@PathVariable Integer caseId){
        List<DatabaseConnect> databaseConnects = databaseConnectService.listDatabaseConnect(caseId);
        return Result.getInstanceList(DatabaseConnect.class).setData(databaseConnects);
    }

    @GetMapping(value = "/state/{databaseId}",name = "数据库操作启用/关闭")
    @ApiOperation(value = "数据库操作启用/关闭",notes = "数据库操作启用/关闭")
    public Result<Boolean> stateDatabaseConnect(@PathVariable String databaseId) throws MainExection {
        return Result.getInstance(Boolean.class).setData(databaseConnectService.stateDatabaseConnect(databaseId));
    }

    @GetMapping(value = "/test/{databaseId}",name = "数据库操作测试")
    @ApiOperation(value = "数据库操作测试",notes = "数据库操作测试")
    public Result<MysqlReturn> testDatabaseConnect(@PathVariable String databaseId) throws MainExection {
        MysqlReturn excult = mysqlUtil.excult(databaseId);
        return Result.getInstance(MysqlReturn.class).setData(excult);
    }


}

